package com.study.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Scanner;

public class BioClient {
    public static void main(String[] args) throws Exception {
        final Socket socket = new Socket("127.0.0.1", 2881);
        new Thread(new ReadMsg(socket)).start();
        PrintWriter pw = new PrintWriter(socket.getOutputStream());
        String msg = "";
        while (!"886".equals(msg)) {
            System.out.println("请输入：");
            Scanner scanner = new Scanner(System.in);
            msg = scanner.next();
            if (!"".equals(msg)) {
                pw.println(msg);
                pw.flush();
            }
        }
        socket.close();
    }

    private static class ReadMsg implements Runnable {

        private Socket socket;

        public ReadMsg(Socket socket) {
            this.socket = socket;
        }

        public void run() {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line = null;
                while ((line = br.readLine()) != null) {
                    System.out.println("客户端接收到消息：" + line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
